package gui_layer;

import java.util.List;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import ctr_layer.SupplierCtr;
import model_layer.Supplier;

@SuppressWarnings("serial")
public abstract class SetSupplierGUI extends UniversalAddWindow
{
	private String[] column_names = { "ID", "Name", "CVR", "Phone Number", "Email", "Address", "Zip Code", "City"};
	private List<Supplier> all_suppliers;
	private SupplierCtr sup_ctr;
	
	public SetSupplierGUI()
	{
		super();
		sup_ctr = new SupplierCtr();
		setTitle("Set Supplier");
		prepare_table(new Object[0][8], column_names);
		setBounds(100, 100, 450, 300);
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		get_non_deleted_suppliers();
		customize();
		this.addInternalFrameListener(new InternalFrameAdapter()
		{
			@Override
			public void internalFrameActivated(InternalFrameEvent arg0)
			{
				dynamically_search();
			}
		});
	}

	private void get_non_deleted_suppliers()
	{
		all_suppliers = sup_ctr.get_non_deleted_suppliers();
	}
	
	private void customize()
	{
		search_lbl.setText("Name/ID");
		btnAdd.setText("Set");
	}

	@Override
	protected void dynamically_search()
	{
		String text = search_field.getText();
		MyTableModel mtm = (MyTableModel) table.getModel();
		Vector<Object[]> vector = new Vector<Object[]>();
		Object[] data;
		for(Supplier sup : all_suppliers)
		{
			if(sup.getName().startsWith(text) || sup.getId().startsWith(text))
			{
				
				data = new Object[8];
				data[0] = Integer.parseInt(sup.getId());
				data[1] = sup.getName();
				data[2] = sup.getCvr();
				data[3] = sup.getPhone_nr();
				data[4] = sup.getEmail();
				data[5] = sup.getAddress();
				data[6] = sup.getZipcode();
				data[7] = sup.getCity();
				vector.add(data);
			}
		}
		Object[][] filling = new Object[vector.size()][8];
		for(int i = 0; i < vector.size(); i++)
		{
			filling[i] = vector.get(i);
		}
		mtm.setDataVector(filling, column_names);
	}

	@Override
	protected void show_non_deleted()
	{
		get_non_deleted_suppliers();
		dynamically_search();

	}

}
